//
//  ZHAvatarUtils.m
//  ZHUIKit
//
//  Created by green on 2018/2/7.
//

#import "ZHAvatarUtils.h"

#import <BCFoundation/UIColor+BCHelper.h>
//#import <BCFoundation/UIImage+BCHelper.h>
#import <BCComConfigKit/BCComConfigKit.h>

@interface ZHAvatarUtils()

@end



@implementation ZHAvatarUtils


//MARK: - 随机色
static NSArray<NSString *> *kZHHelperRandomColors = nil;
+(UIColor *)zh_randomColorWithName:(NSString *)name
{
    if(!kZHHelperRandomColors){
        //随机颜色组从 配置组件里获取
        kZHHelperRandomColors = BCComConfig.config.setting[@"avatar_colors"];
    }
    if (kZHHelperRandomColors.count<=0) {
        kZHHelperRandomColors = @[@"#FDD843", @"#C386DE", @"#85CCBC", @"#98AEEE"];
    }
    if(name.length<=0){
        return [UIColor bc_colorWithRGBString:kZHHelperRandomColors[0]];
    }
    unichar firstChar = [name characterAtIndex:0]; //65
    NSInteger colorIndex = firstChar%kZHHelperRandomColors.count;
    return [UIColor bc_colorWithRGBString:kZHHelperRandomColors[colorIndex]];
}


//+(UIImage *)zh_randomColorImageWithName:(NSString *)name
//{
//    UIImage *randomImg = nil;
//    UIColor *randomColor = [ZHAvatarUtils zh_randomColorWithName:name];
//    CGSize imgSize = CGSizeMake([UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.width);
//    randomImg = [UIImage bc_imageWithColor:randomColor withSize:imgSize withDrawBlock:^(CGSize imgSize) {
//        NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
//        paragraph.alignment = NSTextAlignmentCenter;
//        CGFloat fontSize = [ZHAvatarUtils zh_avatarFontSize:imgSize];
//        CGFloat fontHeight = (kZHFontMedium(fontSize)).lineHeight;
//        NSDictionary *attributes = @{ NSFontAttributeName:kZHFontMedium(fontSize),
//                                      NSForegroundColorAttributeName : [UIColor whiteColor],
//                                      NSParagraphStyleAttributeName : paragraph};
//        NSString *firstName = [name substringToIndex:1];
//        CGRect nameFrame = CGRectMake(0, 0, imgSize.width, imgSize.height);
//        if (fontHeight>0) {
//            fontHeight += 1;
//            nameFrame.origin.y = (imgSize.height-fontHeight)/2.0;
//            nameFrame.size.height = fontHeight;
//        }
//        [firstName drawInRect:CGRectMake(0, (imgSize.height-fontHeight)/2.0, imgSize.width, fontHeight) withAttributes:attributes];
//
//    }];
//    return randomImg;
//}

+ (CGFloat )zh_avatarFontSize:(CGSize )avatarSize withLength:(NSInteger )length
{
    CGFloat fontSize = 0;
    CGFloat avatarWidth = avatarSize.width;
    if (length == 1) {
        fontSize = avatarWidth*0.48;
    } else if (length == 2) {
        fontSize = avatarWidth*0.35;
    } else {
        fontSize = avatarWidth*0.23;
    }
    return fontSize;
}
@end
